package com.example.backend.controller;

import com.example.backend.entity.Port;
import com.example.backend.service.PortService;
import com.example.backend.service.PortRuleCalculationService;
import com.example.backend.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/port")
public class PortController {
    @Autowired
    private PortService portService;
    
    @Autowired
    private PortRuleCalculationService portRuleCalculationService;

    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping
    public ApiResponse<String> addPort(@RequestBody Port port) {
        portService.addPort(port);
        return ApiResponse.success("添加成功");
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping("/{id}")
    public ApiResponse<Port> getPortById(@PathVariable("id") Long id) {
        Port port = portService.getPortById(id);
        if (port != null) {
            return ApiResponse.success(port);
        } else {
            return ApiResponse.error(404, "港口不存在");
        }
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping
    public ApiResponse<List<Port>> getAllPorts() {
        List<Port> ports = portService.getAllPorts();
        return ApiResponse.success(ports);
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @PutMapping
    public ApiResponse<String> updatePort(@RequestBody Port port) {
        portService.updatePort(port);
        return ApiResponse.success("更新成功");
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @DeleteMapping("/{id}")
    public ApiResponse<String> deletePort(@PathVariable("id") Long id) {
        portService.deletePort(id);
        return ApiResponse.success("删除成功");
    }
    
    /**
     * 获取有效的港口规则
     */
    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping("/rule/{companyId}/{portId}")
    public ApiResponse<Object> getEffectiveRule(@PathVariable("companyId") Long companyId, @PathVariable("portId") Long portId) {
        Object[] rule = portRuleCalculationService.getEffectiveRule(companyId, portId);
        Map<String, Object> result = new HashMap<>();
        result.put("freeDays", rule[0]);
        result.put("feeRate", rule[1]);
        return ApiResponse.success(result);
    }
    
    /**
     * 计算存储费用
     */
    @PostMapping("/calculate-fee")
    public ApiResponse<Object> calculateStorageFee(@RequestBody Map<String, Object> request) {
        Long companyId = Long.valueOf(request.get("companyId").toString());
        Long portId = Long.valueOf(request.get("portId").toString());
        BigDecimal weight = new BigDecimal(request.get("weight").toString());
        Integer days = Integer.valueOf(request.get("days").toString());
        
        BigDecimal fee = portRuleCalculationService.calculateStorageFee(companyId, portId, weight, days);
        
        Map<String, Object> result = new HashMap<>();
        result.put("fee", fee);
        result.put("weight", weight);
        result.put("days", days);
        
        return ApiResponse.success(result);
    }
} 